import os
import time
from pathlib import Path
from fnmatch import fnmatch
from typing import Iterator, Optional, Union, List

def get_detailed_dir_info(path='.', recursive=False):
    """
    获取带详细属性的目录和文件信息
    :param path: 文件夹路径
    :param recursive: 是否递归遍历
    :return: 包含完整信息的字典列表
    """
    path = Path(path).absolute()
    result = []

    def scan_directory(current_path):
        with os.scandir(current_path) as entries:
            for entry in entries:
                info = {
                    'name': entry.name,
                    'path': str(Path(entry.path)),
                    'type': 'dir' if entry.is_dir() else 'file',
                    'size': entry.stat().st_size if entry.is_file() else 0,
                    'created': time.ctime(entry.stat().st_ctime),
                    'modified': time.ctime(entry.stat().st_mtime)
                }
                result.append(info)

                if recursive and entry.is_dir():
                    scan_directory(entry.path)

    scan_directory(path)
    return result

def get_files(
        root_dir: str,
        recursive: bool = True,
        include_patterns: Optional[Union[str, List[str]]] = None,
        exclude_patterns: Optional[Union[str, List[str]]] = None,
        yield_absolute_path: bool = True
) -> Iterator[str]:
    """
    获取文件夹下的文件列表（支持递归和过滤）

    :param root_dir: 根目录路径
    :param recursive: 是否递归子目录，默认True
    :param include_patterns: 包含的文件模式（如*.csv），None表示全部包含
    :param exclude_patterns: 排除的文件模式（如temp*），None表示不排除
    :param yield_absolute_path: 是否返回绝对路径，默认返回绝对路径
    :return: 文件路径生成器
    """
    if isinstance(include_patterns, str):
        include_patterns = [include_patterns]
    if isinstance(exclude_patterns, str):
        exclude_patterns = [exclude_patterns]

    for root, _, files in os.walk(root_dir):
        if not recursive and root != root_dir:
            continue  # 非递归模式时跳过子目录

        for filename in files:
            file_path = os.path.join(root, filename)

            # 相对路径处理
            rel_path = os.path.relpath(file_path, start=root_dir)
            output_path = file_path if yield_absolute_path else rel_path

            # 包含过滤器
            if include_patterns and not any(
                    fnmatch(filename, pattern) for pattern in include_patterns
            ):
                continue

            # 排除过滤器
            if exclude_patterns and any(
                    fnmatch(filename, pattern) for pattern in exclude_patterns
            ):
                continue

            yield output_path


if __name__ == '__main__':
    # print(get_detailed_dir_info('e:/'))

    for file_path in get_files('e:/', include_patterns="*.csv"):
        print(file_path)